package two.chapter_1.c1_2;

import java.util.Collection;

public class Transaction implements Comparable<Transaction> {

    private String who;
    private Date when;
    private double amount;
    public Transaction(String transaction){
      this(parse(transaction,0),Date.parse(parse(transaction,1)),
              Double.parseDouble(parse(transaction,2)));

    }
    private static String parse(String transaction,int index){
        String [] fields=transaction.split(" ");
        return fields[index];
    }
    public Transaction(String who,Date when ,double amount){
        this.who=who;
        this.when=when;
        this.amount=amount;
    }

    public String who(){
        return who;
    }
    public Date when(){
        return when;
    }

    public double amount() {
         return amount;
    }

    @Override
    public String toString() {
        return who+ "在"+when+"消费了"+amount;
    }

    @Override
    public boolean equals(Object obj) {
       if (this == obj) return true;
       if (obj==null) return false;

       if (this.getClass()!=obj.getClass()) return false;

       Transaction that= (Transaction) obj;

       if (!who().equals(that.who())) return false;
       if (!when().equals(that.when())) return false;
       if (amount()!=that.amount()) return false;

       return true;
    }

    @Override
    public int compareTo(Transaction o) {
        return 0;
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }
}
